package com.genITeam.ria.actions;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.*;
import com.genITeam.ria.bl.ViewForumBL;
import com.genITeam.ria.exception.NGFException;
import com.genITeam.ria.utility.Utility;
import com.genITeam.ria.vo.Member;
import com.genITeam.ria.vo.Posts;

public class ViewForumAction extends AbstractAction {

	public ViewForumAction() {

		BasicConfigurator.configure();

	}

	Logger logger = Logger.getLogger(ViewForumAction.class);

	/**
	 * handleViewForum to get Parameters from Request object and write resulting
	 * response
	 * 
	 * 
	 * @param HttPServletRequest,HttpServletResponse
	 * @return none
	 * @throws NGFException
	 */
	public void handleViewForum(HttpServletRequest request,
			HttpServletResponse response) throws NGFException {
		ViewForumBL viewForumBL = new ViewForumBL();

		try {
			logger.info("handleViewForum");
			this.initAction(request, response);

			// this.writeResponse(viewForumBL.saveToXmlFile());
			this.writeResponse(viewForumBL.getPost("1"));

		} catch (NGFException e) {
			this.initAction(request, response);
			this.writeResponse("<error>" + e.getErrorMessage() + "</error>");
			System.out.print("Search Forum Excepton = "+ e.getMessage());
		} catch (Exception e) {
			logger.error("Exception=" + e.toString());
			this.initAction(request, response);
			this.writeResponse("<error>" + e.getMessage() + "</error>");

		}

	}

	/**
	 * handleUpdateViewForum to get Parameters from Request object and write
	 * resulting response
	 * 
	 * 
	 * @param HttPServletRequest,HttpServletResponse
	 * @return none
	 * @throws NGFException
	 */

	public void handleUpdateViewForum(HttpServletRequest request,
			HttpServletResponse response) throws NGFException {
		ViewForumBL viewForumBL = new ViewForumBL();
		try {
			logger.info("handleViewForum");
			System.out.println("handleUpdateViewForum");
			this.initAction(request, response);

			String threadid = request.getParameter("threadId");
			System.out.println(threadid);
			String title = request.getParameter("title");
			System.out.println(title);
			String message = request.getParameter("message");
			System.out.println(message);

			Posts posts = new Posts();
			posts.setPostId(Integer.parseInt(threadid));

			Posts postsData = viewForumBL.getForumById(posts);

			// postsData.setPostId(threadid);
			postsData.setTitle(title);
			postsData.setMessage(message);
			// System.out.println("Latest = " + postsData.getMessage());

			viewForumBL.updateForumData(postsData);

			this.initAction(request, response);
			this.writeResponse("<message> Record Updated </message>");

		} catch (Exception e) {
			logger.error("Exception=" + e.toString());
			this.initAction(request, response);
			this.writeResponse("<error>" + e.getMessage() + "</error>");

		}

	}

	/**
	 * handleAddViewForum to get Parameters from Request object and write
	 * resulting response
	 * 
	 * 
	 * @param HttPServletRequest,HttpServletResponse
	 * @return none
	 * @throws NGFException
	 */
	public void handleAddViewForum(HttpServletRequest request,
			HttpServletResponse response) throws NGFException {
		ViewForumBL viewForumBL = new ViewForumBL();
		try {
			logger.info("handleViewForum");
			// System.out.println("handleUpdateViewForum");
			this.initAction(request, response);

			String threadid = request.getParameter("threadId");
			// System.out.println(threadid);
			String title = request.getParameter("title");
			// System.out.println(title);
			String message = request.getParameter("message");
			// System.out.println(message);

			// Posts posts = new Posts();
			// posts.setPostId(Integer.parseInt(threadid));
			//
			// Posts postsData = viewForumBL.getForumById(posts);

			// // ------------------------------------------------
			Member member = new Member();
			member.setId(1);
			// int id = postsData.getPostId();

			Posts pVo = new Posts();
			// System.out.println("hello wrold");

			// pVo.setPostId((Integer.parseInt(postsData.get)));
			pVo.setIsLeaf("true");
			pVo.setTitle(title);
			pVo.setMessage(message);
			pVo.setPostDate(Utility.getCurrentSQLDate());
			pVo.setMember(member);
			// pVo.setPosts(new Posts().setPostId(8));
			Posts p = new Posts();
			p.setPostId(Integer.parseInt(threadid));
			// int pid = p.getPostId();
			pVo.setPosts(p);
			pVo.setPostType('F');
			pVo.setNoOfView(0);
			// ------------------------------------------------

			// postsData.setPostId(threadid);
			// postsData.setTitle(title);
			// postsData.setMessage(message);
			// System.out.println("Latest = " + postsData.getMessage());

			viewForumBL.addForumData(pVo);

			this.initAction(request, response);
			this.writeResponse("<message> Record Added </message>");

		} catch (Exception e) {
			logger.error("Exception=" + e.toString());
			this.initAction(request, response);
			this.writeResponse("<error>" + e.getMessage() + "</error>");

		}

	}

	public static void main(String[] args) {
		try {
			ViewForumBL viewForumBL = new ViewForumBL();
			Posts posts = new Posts();
			posts.setPostId(300);

			Posts postsData = viewForumBL.getForumById(posts);

			// // postsData.setPostId(threadid);
			// postsData.setTitle(title);
			// postsData.setMessage(title);
			postsData.setTitle("Oracle");
			postsData.setMessage("Test");
			System.out.println("Latest = " + postsData.getMessage());

			viewForumBL.updateForumData(postsData);

			// viewForumAction.handleUpdateViewForum(posts);

		} catch (Exception e) {

		}
	}

}
